package com.cloud.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.cloud.train.business.domain.DailyTrain;
import com.cloud.train.business.domain.DailyTrainExample;
import com.cloud.train.business.domain.Train;
import com.cloud.train.business.domain.TrainExample;
import com.cloud.train.business.mapper.DailyTrainMapper;
import com.cloud.train.business.req.DailyTrainQueryReq;
import com.cloud.train.business.req.DailyTrainSaveReq;
import com.cloud.train.business.resp.DailyTrainQueryResp;
import com.cloud.train.common.resp.PageResp;
import com.cloud.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class DailyTrainService {
    @Resource
    private DailyTrainMapper dailyTrainMapper;
    @Resource
    private TrainService trainService;
    @Resource
    private DailyTrainStationService dailyTrainStationService;
    @Resource
    private DailyTrainCarriageService dailyTrainCarriageService;
    @Resource
    private DailyTrainSeatService dailyTrainSeatService;
    @Resource
    private DailyTrainTicketService dailyTrainTicketService;
    public void save(DailyTrainSaveReq req) {
        DateTime now = new DateTime();
        DailyTrain dailyTrain = BeanUtil.copyProperties(req, DailyTrain.class);
        if(ObjectUtil.isNull(dailyTrain.getId())){
            dailyTrain.setId(SnowUtil.getSnowflakeNextId());
            dailyTrain.setCreateTime(now);
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.insert(dailyTrain);
        } else {
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.updateByPrimaryKey(dailyTrain);
        }
    }

    public PageResp<DailyTrainQueryResp> queryList(DailyTrainQueryReq req) {
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        dailyTrainExample.setOrderByClause("date desc, code asc");
        DailyTrainExample.Criteria criteria = dailyTrainExample.createCriteria();
        if (ObjectUtil.isNotNull(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }
        if (ObjectUtil.isNotEmpty(req.getCode())){
            criteria.andCodeEqualTo(req.getCode());
        }
        PageHelper.startPage(req.getPage(),req.getSize());
        List<DailyTrain> dailyTrains = dailyTrainMapper.selectByExample(dailyTrainExample);
        PageInfo<DailyTrain> dailyTrainPageInfo = new PageInfo<>(dailyTrains);
        List<DailyTrainQueryResp> dailyTrainQueryResps = BeanUtil.copyToList(dailyTrains, DailyTrainQueryResp.class);
        PageResp<DailyTrainQueryResp> dailyTrainQueryRespPageResp = new PageResp<>();
        dailyTrainQueryRespPageResp.setTotal(dailyTrainPageInfo.getTotal());
        dailyTrainQueryRespPageResp.setList(dailyTrainQueryResps);
        return dailyTrainQueryRespPageResp;
    }

    public void delete(long id) {
        dailyTrainMapper.deleteByPrimaryKey(id);
    }

    /**生成某日所有信息
     *车次，车站，车厢，座位
     * @param date
     */
    public void genDaily(Date date){
        List<Train> trains = trainService.selectAll();
        if (CollUtil.isEmpty(trains)){
            log.info("没有车次基础数据,任务结束");
            return;
        }
        for (Train train : trains) {
            genDailyTrain(date,train);
        }
    }
    @Transactional
    public void genDailyTrain(Date date,Train train){
        // 删除该车次已有的数据
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        dailyTrainExample.createCriteria()
                .andDateEqualTo(date)
                .andCodeEqualTo(train.getCode());
        dailyTrainMapper.deleteByExample(dailyTrainExample);

        // 生成该车次的数据
        DateTime now = DateTime.now();
        DailyTrain dailyTrain = BeanUtil.copyProperties(train, DailyTrain.class);
        dailyTrain.setId(SnowUtil.getSnowflakeNextId());
        dailyTrain.setCreateTime(now);
        dailyTrain.setUpdateTime(now);
        dailyTrain.setDate(date);
        dailyTrainMapper.insert(dailyTrain);

        // 生成该车次的车站数据
        dailyTrainStationService.genDaily(date, train.getCode());
        //生成车厢数据
        dailyTrainCarriageService.getDaily(date,train.getCode());
        //生成座位数据
        log.info("生成座位数据");
        dailyTrainSeatService.getDaily(date,train.getCode());
        //余票
        log.info("生成余票信息");
        dailyTrainTicketService.genDaily(dailyTrain,date,train.getCode());
    }
}
